package GUI.frame;

import GUI.pojo.UserDO;
import GUI.service.UserService;
import GUI.service.impl.UserServiceImpl;
import GUI.util.DBUtil;
import GUI.util.Utilimg;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPanel extends JPanel implements ActionListener {
    private JButton loginBtn;
    private Text2 frame;
    private ImageIcon image = Utilimg.getIcon("/img/buttonLand.jpg");
    private JTextField jTextArea;
    private JPasswordField iptPassword;
    private int errorcount = 3;
    private UserServiceImpl userService=new UserServiceImpl();
    public JPasswordField getIptPassword() {
        return iptPassword;
    }

    public LoginPanel(Text2 frame) {//接参
        this.frame = frame;//必须赋值，不然会报空指针
        this.setLayout(null);
        this.setOpaque(false);

        JLabel jLabel = new JLabel("账户:");
        jLabel.setBounds(200, 150, 100, 50);
        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel);

        JLabel jLabel2 = new JLabel("密码:");
        jLabel2.setBounds(200, 200, 100, 50);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 30));
        this.add(jLabel2);

        jTextArea = new JTextField();
        jTextArea.setBounds(300, 150, 200, 30);
        this.add(jTextArea);

        iptPassword = new JPasswordField();
        iptPassword.setBounds(300, 200, 200, 30);
        this.add(iptPassword);

        loginBtn = new JButton(image);
        loginBtn.setBounds(300, 380, 100, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginBtn) {
            String account = jTextArea.getText();
            String pwd=String.valueOf(iptPassword.getPassword());
//            for (int i = 0; i < frame.getList().size(); i++) {
//                if (frame.getList().get(i).getAccount().equals(jTextArea.getText()) && frame.getList().get(i).getPwd().equals(jTextArea1.getText())) {
//                    frame.setLoginUser(frame.getList().get(i));//将登录的对象传给loginUser
//                    frame.getChaPanel().Set(frame.getLoginUser().getBalance());//调用到查找类的Set设置方法，传入登录对象的余额
//                    frame.switchPanel("menu");
//                    JOptionPane.showMessageDialog(null, "登录成功");
//                    return;
//                }
//            }

            UserDO user=userService.getByUsernameAndPwd(account,pwd);
            if(user !=null){
                frame.setLoginUser(user);
                frame.switchPanel("menu");
                return;
            }
            errorcount--;
            if(errorcount==0){
                JOptionPane.showMessageDialog(this,"你的银行卡冻结");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "登录失败"+"你还有"+errorcount+"次机会");
        }
    }
}
